package com.chexiehui.jc.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.aries.ui.view.radius.RadiusTextView;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseModel;
import com.chexiehui.jc.base.BaseMvpFragment;
import com.chexiehui.jc.entity.UserInfo;
import com.chexiehui.jc.mvp.mine.ChangePhoneBindCovenant;
import com.chexiehui.jc.mvp.mine.ChangePhoneBindPresenter;
import com.chexiehui.jc.utils.CodeCountDownTimer;
import com.chexiehui.jc.utils.SerializableSpTools;

import butterknife.BindView;
import butterknife.OnClick;

public class ChangePhoneBindFragment extends BaseMvpFragment<ChangePhoneBindPresenter> implements ChangePhoneBindCovenant.View {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_get_code)
    RadiusTextView tvGetCode;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.rtv_next)
    RadiusTextView rtvNext;
    private CodeCountDownTimer phoneLoginTimer;
    private UserInfo userInfo;

    public static ChangePhoneBindFragment getInstance() {
        ChangePhoneBindFragment fragment = new ChangePhoneBindFragment();
        return fragment;
    }

    private PageNextListener pageNextListener;

    @Override
    protected void beforeSetView() {
        userInfo = SerializableSpTools.getUserInfo();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_change_phone_bind;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        rtvNext.setEnabled(false);
    }


    @OnClick({R.id.tv_get_code, R.id.rtv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                mvpPresenter.sendCode();
                break;
            case R.id.rtv_next:
                mvpPresenter.save();
                break;
        }
    }

    @Override
    protected ChangePhoneBindPresenter createPresenter() {
        return new ChangePhoneBindPresenter(this);
    }

    @Override
    public void onSendCodeSuccess(BaseModel<Object> successResult) {
        showToast(String.format(getStringSource(R.string.s_yzmyfs_tips_),
                getInputPhone()));
        if (phoneLoginTimer == null) {
            phoneLoginTimer = new CodeCountDownTimer(tvGetCode, 60000, 1000);
        }
        phoneLoginTimer.start();
        rtvNext.setEnabled(true);
    }

    @Override
    public void onSendCodeFailure(BaseModel<Object> failureResult) {
        showToast(failureResult.getResultInfo());
    }

    @Override
    public void onSaveSuccess(BaseModel<Object> successResult) {
        if(pageNextListener != null){
            pageNextListener.onChangePhoneBindNext(getInputPhone());
        }
    }

    @Override
    public void onSaveFailure(BaseModel<Object> failureResult) {
        showToast(failureResult.getResultInfo());
    }

    @Override
    public String getOldPhone() {
        return userInfo.getTel();
    }

    @Override
    public String getInputPhone() {
        return etPhone.getText().toString().trim();
    }

    @Override
    public String getInputCode() {
        return etCode.getText().toString().trim();
    }

   /* @OnClick(R.id.rtv_next)
    public void onViewClicked() {

    }*/

    public interface PageNextListener {
        void onChangePhoneBindNext(String phone);
    }

    public void setPageNextListener(PageNextListener pageNextListener) {
        this.pageNextListener = pageNextListener;
    }

    @Override
    public void onDestroy() {
        if(phoneLoginTimer != null){
            phoneLoginTimer.release();
            phoneLoginTimer = null;
        }
        super.onDestroy();
    }
}
